package nju.bishe.weibo;

import java.util.HashSet;

import nju.bishe.weibo.data.db.StatusTable;
import nju.bishe.weibo.data.db.TwitterDatabase;
import nju.bishe.weibo.helper.ImageManager;
import nju.bishe.weibo.helper.Preferences;
import nju.bishe.weibo.helper.ProfileImageCacheManager;
import weibo4andriod.Configuration;
import weibo4andriod.Weibo;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.StrictMode;
import android.preference.PreferenceManager;
import android.util.Log;
import android.widget.Toast;

public class TwitterApplication extends Application {

	public static final String TAG = "TwitterApplication";

	// public static ImageManager mImageManager;
	public static ProfileImageCacheManager mProfileImageCacheManager;
	public static TwitterDatabase mDb;
	public static Weibo mApi; // new API
	public static Context mContext;
	public static SharedPreferences mPref;

	public static int networkType = 0;

	public static ImageManager mImageManager;

	private final static boolean DEBUG = Configuration.getDebug();

	// 获取登录用户id
	public static String getMyselfId() {
		
		Log.w(TAG, TwitterApplication.mPref.getString(Preferences.CURRENT_USER_ID,
				TwitterApplication.mApi.getUserId()));
		
		return TwitterApplication.mPref.getString(Preferences.CURRENT_USER_ID,
				TwitterApplication.mApi.getUserId());
	}

	@Override
	public void onCreate() {
		// NOTE: StrictMode模式需要2.3 API支持。
	/*	if (DEBUG) {
			StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
					.detectAll().penaltyLog().build());
			StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
					.detectAll().penaltyLog().build());
		}
*/
		super.onCreate();

		mContext = this.getApplicationContext();
		// mImageManager = new ImageManager(this);
		mProfileImageCacheManager = new ProfileImageCacheManager();
		mApi = new Weibo();
		mDb = TwitterDatabase.getInstance(this);

		mPref = PreferenceManager.getDefaultSharedPreferences(this);
	/*	String username = mPref.getString(Preferences.USERNAME_KEY, "");
		String accesstoken = mPref.getString(Preferences.ACCESSTOKEN, "");
		String accesstoken_sercet = mPref.getString(
				Preferences.ACCESSTOKEN_SECRET, "");*/
		// String password = mPref.getString(Preferences.PASSWORD_KEY, "");

		// if (Weibo.isValidCredentials(username, password)) {
		// mApi.setCredentials(username, password); // Setup API and HttpClient
		// }
	//	mApi.setToken(accesstoken, accesstoken_sercet);
		// 为cmwap用户设置代理上网
		
	}

	

	@Override
	public void onTerminate() {
		cleanupImages();
		mDb.close();
		Toast.makeText(this, "exit app", Toast.LENGTH_LONG);

		super.onTerminate();
	}

	private void cleanupImages() {
		HashSet<String> keepers = new HashSet<String>();

		Cursor cursor = mDb.fetchAllTweets(StatusTable.TYPE_HOME);

		if (cursor.moveToFirst()) {
			int imageIndex = cursor
					.getColumnIndexOrThrow(StatusTable.FIELD_PROFILE_IMAGE_URL);
			do {
				keepers.add(cursor.getString(imageIndex));
			} while (cursor.moveToNext());
		}

		cursor.close();

		cursor = mDb.fetchAllDms(-1);

		if (cursor.moveToFirst()) {
			int imageIndex = cursor
					.getColumnIndexOrThrow(StatusTable.FIELD_PROFILE_IMAGE_URL);
			do {
				keepers.add(cursor.getString(imageIndex));
			} while (cursor.moveToNext());
		}

		cursor.close();

		mProfileImageCacheManager.getImageManager().cleanup(keepers);
	}
}
